package com.example.forum.utils;

/*
    发送邮件的工具
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtils {
    private static final Logger logger= LoggerFactory.getLogger(MailUtils.class);
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    /**
     *
     * @param to : 接收者
     * @param title: 标题
     * @param content : 内容
     */
    public  void sendMail(String to,String title,String content){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(title);
            helper.setText(content,true);

            //发送邮件
            javaMailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("邮件发送失败:"+e.getMessage());
        }
    }

}
